/*
 * \brief  Framebuffer driver interface, X86emu interface
 * \author Christian Helmuth
 * \author Sebastian Sumpf
 * \date   2006-09-11
 */

/*
 * Copyright (C) 2006-2009 Christian Helmuth
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _FRAMEBUFFER_H
#define _FRAMEBUFFER_H

#include <base/capability.h>
#include <base/stdint.h>

namespace Framebuffer_drv {

	/**
	 * Return capability for h/w framebuffer dataspace
	 */
	Genode::Dataspace_capability hw_framebuffer();

	/**
	 * Initialize driver, x86emu lib, set up memory
	 */
	int init();

	/**
	 * Set video, initialize framebuffer dataspace
	 *
	 * \return  0 on success,
	 *          non-zero otherwise
	 */
	int set_mode(unsigned long width, unsigned long height, unsigned long mode);

	/**
	 * Map given device memory, return out_addr (map address)
	 */
	int map_io_mem(Genode::addr_t base, Genode::size_t size,
	               void **out_addr, Genode::addr_t addr = 0,
	               Genode::Dataspace_capability *out_io = 0);

}


namespace X86emu {

	using Genode::addr_t;
	using Genode::uint16_t;

	/**
	 * Emulation memory/code area structure
	 */
	struct X86emu_mem
	{
			addr_t _bios_addr;
			addr_t _data_addr;
			addr_t _vga_addr;

			/**
			 * Constructur
			 */
			X86emu_mem() : _bios_addr(0), _data_addr(0), _vga_addr(0) {}

			inline addr_t _addr(addr_t* obj_addr, void* addr = 0)
			{
				if(addr != 0)
					*obj_addr = reinterpret_cast<addr_t>(addr);
				return *obj_addr;
			}

			/**
			 * set/get functions
			 */
			inline addr_t bios_addr(void *addr = 0) {
				return _addr(&_bios_addr, addr); }
			inline addr_t data_addr(void *addr = 0) {
				return _addr(&_data_addr, addr); }
			inline addr_t vga_addr(void *addr = 0) {
				return _addr(&_vga_addr,  addr); }
	};

	/**
	 * Memory/code area instance
	 */
	extern struct X86emu_mem x86_mem;

	/**
	 * Initialization
	 */
	int init(void);

	/**
	 * Execute real mode command
	 */
	uint16_t x86emu_cmd(uint16_t eax, uint16_t ebx = 0, uint16_t ecx = 0,
	                    uint16_t edi = 0);

	/**
	 * Map requested address to local virtual address
	 */
	template <typename OUT, typename IN>
	OUT virt_addr(IN addr);

	/**
	 * Ouput ports
	 */
	void verbose();
}
#endif /* _FRAMEBUFFER_H_ */
